var Router = require('koa-router')
var request = require('koa-request')
var parseString = require('xml2js').parseString

var router = new Router()

function *xml2json(xml, next){
    parseString(xml, function(err, result){
        return JSON.stringify(result)
    })
}

router
    .param('page', function *(page, next){
        this.page = page

        yield next
    })
    .param('size', function *(size, next){
        this.size = size

        yield next
    })
    .get('/essence/:page/:size', function *(next){

        var blogRes = yield request({
            url: 'http://wcf.open.cnblogs.com/blog/bloggers/recommend/'+this.page+'/'+this.size
        })

        var _this = this

        parseString(blogRes.body, { explicitArray : false, ignoreAttrs : true }, function(err, result){

            var json = JSON.stringify(result)

            _this.response.type = "application/json"

            _this.body = json
        })
    })

module.exports = router